/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.losalpes.marketplace.pitrack.beans;

import co.com.losalpes.marketplace.pitrack.bos.TrackBO;
import co.com.losalpes.marketplace.pitrack.entities.Track;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author marketplace
 */
@Stateful
public class TRACKBeanTest implements TRACKRemote, TRACKLocal {

    @PersistenceContext
    private EntityManager em;

    /**
     * Permite Crear una transaccion que se esta ejecutando en el marketplace
     *
     * @param trackBO
     * @return en numero de la transacciín
     */
    @Override
    public String crearTrack(TrackBO trackBO) {
        String numero = "TRACK" + System.currentTimeMillis();
        trackBO.setIdTransaccion(numero);
        Track track = new Track(trackBO);
        em.persist(track);
        return numero;
    }

    /**
     * Busco el último track, siendo diferentes los fabricantes
     *
     * @param nit
     * @return
     */
    public List<TrackBO> buscarPorUltimoTimeIdTransaccionDiferente(String nit) {
        
        TrackBO trackBO = null;
        List<TrackBO> trackList = new ArrayList<TrackBO>(0);
        List<Track> tracks = null;
        Query query = em.createNamedQuery("findTrackList");
        query.setParameter("nitFabricante", nit);
        
        if(query.getResultList() != null && !query.getResultList().isEmpty()){
            tracks = new ArrayList<Track>();
            tracks = query.getResultList();
            
            for(Track track : tracks){
                trackBO = new TrackBO();
                trackBO = track.TrackBO();
                trackList.add(trackBO);
            }
            
        }

        return trackList;
    }

    /**
     * Permite consultar una transacción por su identificador
     * @param idTransaccion
     * @return 
     */
    public TrackBO buscarTransaccionPorId(String idTransaccion) {
        TrackBO trackBO = new TrackBO();
        Query query = em.createNamedQuery("findByIdTrack");
        query.setParameter("idTransaccion", idTransaccion);

        if (query.getResultList() != null && !query.getResultList().isEmpty()) {
            Track track = (Track) query.getResultList().get(0);
            trackBO = track.TrackBO();
        }
        return trackBO;
    }

    public List<TrackBO> buscarPorNumSeguimiento(String numSeguimiento) {
        TrackBO trackBO = null;
        List<TrackBO> trackList = new ArrayList<TrackBO>(0);
        List<Track> tracks = null;
       
       Query query = em.createNamedQuery("findTrackListByNumSeg");
       query.setParameter("numSeguimiento", numSeguimiento);
       
       if (query.getResultList() != null && !query.getResultList().isEmpty()) {
            tracks = new ArrayList<Track>();
            tracks = query.getResultList();
            
            for(Track track : tracks){
                trackBO = new TrackBO();
                trackBO = track.TrackBO();
                trackList.add(trackBO);
            }
           
       }
       
       return trackList;        
    }
}
